const express = require('express');
const router = require('./routes/good');
const bodyParser = require('body-parser');
const cors = require('cors');
const path = require('path');
var cookieParser = require('cookie-parser');
var app = express();

app.use(cors({
    origin: true,
    credentials: true,
}));
app.use(cookieParser());
app.use(function(req,res,next){
    if(req.cookies.userId){
        next();
    }else{
        if(req.originalUrl=='/list' || req.originalUrl == '/getGoodsSort' || req.originalUrl.indexOf('/user')>-1 || req.originalUrl == '/checkLogin'){
            next();
        }else{
            res.json({
                status:'10001',
                msg:'当前未登录',
                result:''
            });
        }
    }
})

// parse application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: false }));
// parse application/json
app.use(bodyParser.json());



app.use('/node_modules/',express.static(path.join(__dirname,"./node_modules/")));
app.use('/public/',express.static(path.join(__dirname,"./public/")));
app.use(router);

app.listen(3333,() =>{
    console.log('server is running port 3333');
})